# DISCUSSIONS ---------------------------------------------------------------

# Reasons for choices (in discussion) ---------------------------------------------------------------

# Get the common reasons and amalgamate reasons where appropriate
reasons_clean <- reasons_discussion %>%
  # select(reason_text, reason_selected) %>%
  tidylog::left_join(harmonise_reasons_collapse, by = "reason_text") %>%
  group_by(across(c(group_id, round, category, reason_text_collapse, group_obs_trans_included, any_of(id_vars)))) %>%
  summarise(
    reason_selected = any(reason_selected)
  ) %>%
  rename(reason_text = reason_text_collapse) %>%
  mutate(reason_text = reason_text %>%
    str_replace_all("I want to give opportunity / help the worker", "     I want to give opportunity / help the worker"))

# ALL REASONS
reasons_clean %>%
  filter(discuss_type == "discussion_full") %>%
  bar_chart(x = reason_text, y = reason_selected, fill = group_obs_trans_included, facet = category,
            return_data = TRUE) %>%
  group_by(reason_text) %>%
  mutate(reason_text = coalesce(reason_text, "(Other reason)")) %>%
  mutate(sort = y[group_obs_trans_included]) %>%
  mutate(sort = ifelse(reason_text == "(Other reason)", 0, sort)) %>%
  arrange(sort, reason_text, group_obs_trans_included) %>%
  mutate(reason_text = str_replace(reason_text, "^\\d\\d?\\:\\s", "") %>%
    str_replace("It's a ", "Worker is ") %>%
    str_replace("lady", "woman") %>%
    str_replace("the person has a ", "")) %>%
  mutate(
    label = factor(reason_text, levels = unique(.$reason_text))
  ) %>%
  mutate(
    group_obs_trans_included = ifelse(as.character(group_obs_trans_included), "Choice includes\ntrans", "Choice does not\ninclude trans")
  ) %>%
  mutate(
    category = fct_relevel(factor(category), c("Gender", "Pro-social", "Items", "Worker", "Worker details", "Negative", "Other reason"))
  ) %>%
  ungroup %>%
  mutate(
    text_cat = interaction(reason_text, category) %>%
      fct_relevel(
        "Other reason.Other reason",
        "Negative about other worker.Negative",
        "Education.Worker details",
        "Experience.Worker details",
        "Speaks English.Worker details",
        "Reliability Score.Worker details",
        "Age.Worker",
        "Worker seems friendly.Worker",
        "Easy to relate to worker.Worker",
        "Worker seems reliable.Worker",
        "How the worker looks / photo.Worker",
        "Easy to talk with worker.Worker",
        "Better items.Items",
        "We shouldn't discriminate.Pro-social",
        "Worker seems poor.Pro-social",
        "They are also human.Pro-social",
        "     I want to give opportunity / help the worker.Pro-social",
        "Worker is female.Gender",
        "Worker is male.Gender",
        "Worker is transgender.Gender"
      )
  ) %>%

  ggplot(aes(x = text_cat, y = y, fill = group_obs_trans_included)) +
  geom_col(
    width = 0.5, position = position_dodge(0.5)
  ) +
  scale_x_discrete(guide = guide_axis_nested(n.dodge = 1)) +
  theme(panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "top") +
  labs(x = element_blank(), y = "% of choice-pairs with this reason cited", fill = "Pair type") +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), expand = c(0, 0), limits = c(0, 0.75)) +
  coord_flip()

ggsave("outputs/figs/reasons_r1_full.pdf", width = 8, height = 7, scale = 0.9)

# Likelihood of each category of reasons ---------------------------------------------------------------

category_order <- c("Gender", "Pro-social", "Items", "Worker", "Worker details", "Negative", "Other reason")

# Get reasons clean to the category level
reasons_clean_cat <- reasons_clean %>%
  group_by(across(c(group_id, round, category, group_obs_trans_included, all_of(id_vars)))) %>%
  summarise(
    reason_selected = any(reason_selected)
  ) %>%
  ungroup %>%
  relocate(reason_selected) %>%
  mutate(category = factor(category)) %>%
  mutate(category = fct_rev(fct_relevel(category, category_order))) %>%
  mutate(group_obs_trans_included_label = ifelse(
    group_obs_trans_included, "Choice includes trans", "Choice doesn't include trans"
  ) %>% factor() %>% fct_relevel("Choice includes trans"))


effect_pro_social <- feols_custom(
  reason_selected ~ group_obs_trans_included,
  data = reasons_clean_cat %>% filter(category == "Pro-social"),
)

effect_pro_social %>% get_p_val("group_obs_trans_includedTRUE") %>% write_stat("outputs/stats/effect_pro_social_p.tex", p_value = TRUE)
effect_pro_social %>% get_coeff("group_obs_trans_includedTRUE") %>% times_100 %>% write_stat("outputs/stats/effect_pro_social.tex", digits = 1)


plot_reasons_cat <- reasons_clean_cat %>%
  bar_chart(x = category, y = reason_selected, fill = group_obs_trans_included_label, perc = TRUE, flip = TRUE) +
  labs(fill = element_blank(), x = "Reason category", y = "Proportion of choice-pairs with this reason cited") +
  scale_fill_manual(values = c(gg_color_hue(2)[[2]], gg_color_hue(2)[[1]])) +
  theme(legend.position = "top")


print(plot_reasons_cat)


plot_reasons_cat_by_choice <- reasons_clean_cat %>%
  mutate(round = as.numeric(round)) %>%
  tidylog::left_join(r1_dominates_group_id, by = c("group_id", "round"), suffix = c("", "_extra")) %>%

  ungroup %>%
  count_prop(
    group_obs_trans_included, r1_choose_trans
  ) %>%

  mutate(r1_choose_trans_label = case_when(
    is.na(r1_choose_trans) ~ "Choice doesn't include trans",
    r1_choose_trans ~ "Chose transgender worker",
    !r1_choose_trans ~ "Didn't choose transgender worker"
  ) %>%
    factor() %>% fct_relevel(c("Chose transgender worker", "Didn't choose transgender worker", "Choice doesn't include trans"))) %>%


  bar_chart(x = category, y = reason_selected, fill = r1_choose_trans_label, flip = TRUE, perc = TRUE) +
  labs(x = "Reason category", y = "Proportion of choice-pairs with this reason cited", fill = element_blank()) +
  scale_fill_manual(values = c("#6ec5ff", "#004cbf", gg_color_hue(2)[[1]])) +
  theme(legend.position = "top")

ggarrange(
  plot_reasons_cat,
  plot_reasons_cat_by_choice,
  labels = "AUTO"
)

# V2: - all in one graph
plot_data_reasons_cat_1 <- reasons_clean_cat %>%
  tidylog::filter(discuss_type == "discussion_full") %>%
  bar_chart(x = category, y = reason_selected, fill = group_obs_trans_included_label, perc = TRUE, flip = TRUE, return_data = TRUE)

plot_data_reasons_cat_2 <- reasons_clean_cat %>%
  tidylog::filter(discuss_type == "discussion_full") %>%
  mutate(round = as.numeric(round)) %>%
  tidylog::left_join(r1_dominates_group_id, by = c("group_id", "round"), suffix = c("", "_extra")) %>%

  ungroup %>%

  ungroup %>%
  mutate(
    r1_choose_trans_label = case_when(
      is.na(r1_choose_trans) ~ "Choice doesn't include trans",
      r1_choose_trans ~ "Choice includes trans & chose transgender worker",
      !r1_choose_trans ~ "Choice includes trans & didn't choose transgender worker"
    ) %>%
      factor() %>%
      fct_relevel("Choice includes trans & chose transgender worker", "Choice includes trans & didn't choose transgender worker", "Choice doesn't include trans")) %>%
  bar_chart(x = category, y = reason_selected, fill = r1_choose_trans_label, flip = TRUE, perc = TRUE, return_data = TRUE)




bind_rows(
  plot_data_reasons_cat_1,
  plot_data_reasons_cat_2 %>% filter(r1_choose_trans_label != "Choice doesn't include trans")
) %>%
  mutate(
    type = coalesce(as.character(group_obs_trans_included_label), as.character(r1_choose_trans_label))
  ) %>%

  mutate(
    type = fct_rev(fct_relevel(type, "Choice doesn't include trans", "Choice includes trans", "Choice includes trans & chose transgender worker", "Choice includes trans & didn't choose transgender worker"))
  ) %>%
  ggplot(aes(x = category, y = y, fill = type)) +
  geom_col_pattern(aes(pattern = type, pattern_colour = type, pattern_fill = type, colour = type),
                   pattern_size = 0,
                   pattern_density = 0.4,
                   pattern_spacing = 0.015,
                   position = position_dodge(0.8), width = 0.8) +
  geom_errorbar(aes(ymin = ymin, ymax = ymax), width = 0.2, position = position_dodge(0.8)) +
  coord_flip() +
  scale_pattern_manual(
    values = c("stripe", "circle", "stripe", "stripe"),
    guide = guide_legend(reverse = TRUE)

  ) +
  scale_colour_manual(
    values = c(gg_color_hue(2)[[2]], gg_color_hue(2)[[2]], gg_color_hue(2)[[2]], gg_color_hue(2)[[1]]),
    guide = guide_legend(reverse = TRUE)

  ) +
  scale_pattern_fill_manual(
    values = c(gg_color_hue(2)[[2]], gg_color_hue(2)[[2]], gg_color_hue(2)[[2]], gg_color_hue(2)[[1]]),
    guide = guide_legend(reverse = TRUE)

  ) +
  scale_pattern_colour_manual(
    values = c(gg_color_hue(2)[[2]], gg_color_hue(2)[[2]], gg_color_hue(2)[[2]], gg_color_hue(2)[[1]]),
    guide = guide_legend(reverse = TRUE)

  ) +
  scale_fill_manual(
    values = c("white", "white", gg_color_hue(2)[[2]], gg_color_hue(2)[[1]]),
    guide = guide_legend(reverse = TRUE)
  ) +
  theme_bw() +
  theme(
    legend.position = c(0.72, 0.15),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank()) +
  labs(y = "% of choices with this reason cited", x = "Reason category",
       fill = element_blank(),
       colour = element_blank(),
       pattern_colour = element_blank(),
       pattern_fill = element_blank(),
       pattern = element_blank()) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1))



ggsave("outputs/figs/reasons_r1_categories.pdf", width = 9, height = 6.5, scale = 0.8)

# Effect of dominate status on narratives/reasons ---------------------------------------------------------------

reasons_clean_cat %>%
  mutate(round = as.numeric(round)) %>%
  tidylog::left_join(r1_dominates_group_id, by = c("group_id", "round"), suffix = c("", "_extra")) %>%
  ungroup %>%

  bar_chart(x = category, y = reason_selected, fill = dominate_status, facet = group_obs_trans_included)


# Not much effect of dominating status on narratives / reasons cited in the discussion


# Proporotion of positive/negative mentions of trans ---------------------------------------------------------------

discuss_obs %>%
  pivot_longer(c(pos_mentions, neg_mentions)) %>%
  group_by(name) %>%
  filter(discuss_type == "discussion_full") %>%
  mutate(
    name = recode(name,
                  "neg_mentions" = "Negative statements\nabout transgender",
                  "pos_mentions" = "Positive statements\nabout transgender")
  ) %>%
  summarise(mean_cl_boot(value), n = n()) %>%

  ggplot(aes(x = name, y = y, fill = name, ymin = ymin, ymax = ymax)) +
  scale_fill_manual(values = c("#026873", "#03A688")) +
  geom_col(show.legend = FALSE) +
  geom_errorbar(width = 0.2, colour = "#333333", alpha = 0.7) +
  geom_text(aes(y = 0.03, x = name, label = str_glue("{round(y,2)*100}%")), position = position_dodge(0.8), colour = "white", size = 4) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  labs(x = element_blank(), y = "% of discussions about pairs\nwith transgender option:")

ggsave("outputs/figs/prob_positive_mentions.pdf", width = 4, height = 5)


mean_pos_mentions <- discuss_obs %>%
  filter(group_obs_trans_included, discuss_type == "discussion_full") %>%
  get_mean(pos_mentions)

mean_pos_mentions %>% write_percentage("outputs/stats/mean_pos_mentions.tex", digits = 0)

mean_neg_mentions <- discuss_obs %>%
  filter(group_obs_trans_included, discuss_type == "discussion_full") %>%
  get_mean(neg_mentions)

mean_neg_mentions %>% write_percentage("outputs/stats/mean_neg_mentions.tex", digits = 0)


(mean_pos_mentions / mean_neg_mentions) %>% write_stat("outputs/stats/ratio_pos_neg_mentions.tex", digits = 1)


# Instrumenting R1 choices with quality and seeing effect on R2 ---------------------------------------------------------------

# DOESN'T WORK! Not enoguh power / confusing results

feols(
  r1_choose_trans ~ item_diff + quality_diff,
  data = r1_choices %>% filter(discuss_type == "discussion_full", pair_includes_trans==1),
  fixef = c("stratum_id")
)

r1_choices %>%
  filter(discuss_type == "discussion_full", pair_includes_trans==1) %>%
  count_prop(round)

# Summarise to one per person
feols(
  n_r1_choose_trans ~ item_diff_mean + quality_diff_mean + educ_diff_mean + english_diff_mean + discuss_type,
  data = r1_n_choose_trans %>% filter(discuss_type %in% c("control", "discussion_full")),
  fixef = c("stratum_id")
)


# Heterogeneity by whether others said something pro-trans in discussion ---------------------------------------------------------------

heterogeneity_discussions <- list(
  "Selected transgender in outcome round\n(3-person discussion only)" = feols_custom(
    r2_choose_trans ~  gobs9_z + gobs9_z_group_excl,
    data = r2_w_discuss_het %>% filter(discuss_type == "discussion_full", pair_includes_trans),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id"
  ),
  "Selected transgender in outcome round\n(3-person discussion only)" = feols_custom(
    r2_choose_trans ~  spoke_pro_trans + spoke_pro_trans_group_excl,
    data = r2_w_discuss_het %>% filter(discuss_type == "discussion_full", pair_includes_trans),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id"
  ),
  "Selected transgender in outcome round\n(3-person discussion only)" = feols_custom(
    r2_choose_trans ~  favourable_discussion_z + favourable_discussion_z_group_excl + stratum_id,
    data = r2_w_discuss_het %>% filter(discuss_type == "discussion_full", pair_includes_trans),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id"
  )
)


tex_export(
  heterogeneity_discussions,
  file = "outputs/tables/r2_heterogeneity_by_discussions.tex",
  coef_rename = coef_label,
  column_widths = list(4, "1.5in"),
  gof_map = fe_label,
  coef_omit = vars_to_regex(c("stratum_id", "Intercept", "video", str_subset(control_vars, "r2_reliability", negate = TRUE), "delivery")),
)


heterogeneity_discussions[[3]] %>%
  tidy_90 %>%
  filter(!str_detect(term, "stratum|Intercept")) %>%
  mutate(term = term %>% str_replace(
    "favourable_discussion_z_group_excl", "Pro-trans discussion by others in group\n(Z-score index)"
  ) %>%
    str_replace(
      "favourable_discussion_z", "Pro-trans discussion by self\n(Z-score index)"
    )
  ) %>%
  ggplot(aes(x = term), show.legend = FALSE) +
  geom_hline(yintercept = 0, linetype = "dashed", colour = "skyblue", show.legend = FALSE) +
  geom_errorbar(aes(ymin = conf.low_90, ymax = conf.high_90), colour = "#636363", width = 0, size = 1.2, show.legend = FALSE) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), colour = "#636363", width = 0, size = 0.7, show.legend = FALSE) +
  geom_point(aes(y = estimate, colour = p.value <= 0.05), show.legend = F, size = 3) +
  coord_flip() +
  theme_minimal() +
  theme(panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.minor.x = element_blank()) +
  labs(y = "Coefficient estimate on:\nP(selected transgender in outcome round)\n for group discussion participants",
       x = element_blank())

ggsave("outputs/figs/heterogeneity_by_discussion.pdf", width = 6, height = 3)




# Compare discussions with no pro-trans discussion to no-discussion people ---------------------------------------------------------------


r2_w_discuss_het_control_mean <- r2_w_discuss_het %>% filter(discuss_type == "control") %>%
  pull(r2_choose_trans) %>% mean_na()



plot_heterogeneity_by_positive_discussions <- r2_w_discuss_het %>%
  filter(discuss_type == "discussion_full") %>%
  mutate(spoke_pro_trans_group = round(spoke_pro_trans_group, 3)) %>%
  group_by(spoke_pro_trans_group) %>%
  summarise(mean_cl_cluster(r2_choose_trans, cluster = group_id), n = n()) %>%
  ungroup %>%

  ggplot(aes(x = spoke_pro_trans_group, y = estimate)) +
  geom_point(colour = colour_green) +
  geom_line(colour = colour_green) +
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high), colour = colour_green, alpha = 0, width = 0, linetype = "dashed") +

  geom_hline(
    yintercept = r2_w_discuss_het_control_mean,
    colour = colour_orange
  ) +
  annotate(
    geom = "label",
    y = r2_w_discuss_het_control_mean - 0.15,
    x = 0.5,
    label = str_glue("No discussion (private)\nMean = {round(r2_w_discuss_het_control_mean, 2)*100}%"),
    colour = colour_orange,
    size = 3,
    hjust = 0,
    vjust = 0
  ) +
  annotate(
    geom = "text",
    y = 0.8,
    x = 0.5,
    label = str_glue("3-person discussion"),
    colour = colour_green,
    size = 3,
    hjust = 0,
    vjust = 0
  ) +
  labs(x = "Probability that a discussion participant\ndiscussed transgender positively\nin discussion (transgender choices only)",
       y = "Probability of selecting\ntransgender in outcome round") +
  theme_classic() +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), expand = c(0, 0), lim = c(0, 1.02)) +
  scale_x_continuous(labels = scales::percent_format(accuracy = 1), lim = c(0, 1.02)) +
  theme(plot.margin = unit(c(0, 1, 0, 1), "cm"))

plot_heterogeneity_by_positive_discussions

# P-value for difference between control group and discussion groups with no positive discussion at all
estimate_diff_control_anti_discuss <- r2_w_discuss_het %>%
  count_prop(spoke_pro_trans_group) %>%
  filter(discuss_type %in% c("control", "discussion_full")) %>%
  count_prop(discuss_type, spoke_pro_trans_group) %>%
  ungroup %>%
  filter(discuss_type == "control" | spoke_pro_trans_group %in% 0) %>%

  feols(
    r2_choose_trans ~ discuss_type,
    cluster = "group_id",
    data = .
  ) %>%
  tidy_90 %>%
  filter(str_detect(term, "discuss_type"))


estimate_diff_control_anti_discuss$estimate %>% write_stat("outputs/stats/diff_control_anti_discuss.tex", digits = 2)
estimate_diff_control_anti_discuss$p.value %>% write_stat("outputs/stats/diff_control_anti_discuss_p.tex", digits = 2)


# Compare choices by how many times trans picked in first discussion ---------------------------------------------------------------

# How many times was trans picked?

# People who are in groups that don't choose trans are way more likely to discriminate than individuals
plot_heterogeneity_by_r1_choices <- r2_choices %>%
  tidylog::left_join(r1_n_choose_trans, by = "ind_id", suffix = c("", "_extra")) %>%

  filter(discuss_type == "discussion_full") %>%

  group_by(n_r1_choose_trans_group) %>%
  summarise(mean_cl_cluster(r2_choose_trans, cluster = group_id), n = n()) %>%
  ungroup %>%

  ggplot(aes(x = n_r1_choose_trans_group, y = estimate)) +
  geom_hline(
    yintercept = r2_w_discuss_het_control_mean,
    colour = colour_orange
  ) +
  geom_point(size = 2, colour = colour_green) +
  geom_line(colour = colour_green, linetype = "dotted") +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), size = 1, colour = colour_green, width = 0) +

  labs(x = "# of times trans was selected during discussion\n\n",
       y = element_blank()) +
  theme_classic() +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), expand = c(0, 0), lim = c(0, 1.02)) +
  scale_x_continuous(breaks = c(0, 1, 2)) +
  annotate(
    geom = "label",
    y = r2_w_discuss_het_control_mean - 0.15,
    x = 0.9,
    label = str_glue("No discussion (private)\nMean = {round(r2_w_discuss_het_control_mean, 2)*100}%"),
    colour = colour_orange,
    size = 3,
    hjust = 0,
    vjust = 0
  ) +
  annotate(
    geom = "text",
    y = 0.75,
    x = 1,
    label = str_glue("3-person discussion"),
    colour = colour_green,
    size = 3,
    hjust = 0,
    vjust = 0
  ) +
  theme(plot.margin = unit(c(0, 1, 0, 1), "cm"))

plot_heterogeneity_by_r1_choices

ggsave("outputs/figs/heterogeneity_by_r1_choices.pdf", width = 5, height = 5)


# Combine both plots
ggarrange(
  plot_heterogeneity_by_positive_discussions,
  plot_heterogeneity_by_r1_choices,
  labels = c("A", "B"),
  hjust = -9,
  vjust = 3,
  widths = c(1.1, 1)
)

ggsave("outputs/figs/heterogeneity_by_r1.pdf", width = 10, height = 4.5, scale = 0.8)

# How much discussion occurred in each pair ---------------------------------------------------------------

discuss_obs %>%
  filter(discuss_type == "discussion_full") %>%
  mutate(group_obs_trans_included = as.numeric(group_obs_trans_included)) %>%
  bar_chart(x = group_obs_trans_included, y = amount_discussed_z)

x_0 <- geom_vline(xintercept = 0)


df %>%
  count_prop(gobs8) %>%
  bar_chart(x = video_type, y = gobs8)


amount_discussed <- list(
  "3-person\ndiscussion only\n(Phases 1 + 2)" = feols_custom(
    amount_discussed_z ~ group_obs_trans_included,
    data = discuss_obs %>% filter(discuss_type == "discussion_full"),
    fixef = c("stratum_id", "video_type"), cluster = "group_id"
  ),
  "2-person\ndiscussion only\n(Phase 2 only)" = feols_custom(
    amount_discussed_z ~ group_obs_trans_included,
    data = discuss_obs %>% filter(discuss_type == "discussion_pair"),
    fixef = c("stratum_id", "video_type"), cluster = "group_id"
  ),
  "2- & 3-person\ndiscussions\n(Phase 2 only) " = feols_custom(
    amount_discussed_z ~ group_obs_trans_included + discussion_full,
    data = discuss_obs %>% filter(phase == "phase_2", group_role == 2, discuss_type %in% c("discussion_full", "discussion_pair")),
    fixef = c("stratum_id", "video_type"), cluster = "group_id"
  ),
  "2- & 3-person\ndiscussions\n(Phase 2 only)" = feols_custom(
    gobs8_z ~ discussion_full,
    data = discuss_obs_per_group_phase2,
    fixef = c("stratum_id", "video_type"), cluster = "group_id"
  )
)


tex_export(
  amount_discussed,
  file = "outputs/tables/amount_discussed.tex",
  coef_rename = coef_label,
  column_widths = list(4, "1.5in"),
  gof_map = fe_label_no_fe %>% filter(raw != "N participants"),
  dep_means = list(
    "Mean: Choice doesn't include trans" = "!group_obs_trans_included",
    "Mean: 2-person discussion" = "discuss_type == 'discussion_pair'",
    "Mean: 3-person discussion" = "discuss_type == 'discussion_full'"
  ),
  controls_row = TRUE,
  additional_header = vec_to_custom_header(
    c(" ", rep("Amount discussed for each choice (Z)", 3), "Amount discussed overall\nin a group (Z)")
  )
)

amount_discussed[[3]] %>% get_coeff("discussion_full") %>% write_stat("outputs/stats/amount_discussed_diff.tex")
amount_discussed[[3]] %>% get_p_val("discussion_full") %>% write_stat("outputs/stats/amount_discussed_diff_p.tex", digits = 3, p_value = TRUE)


# Heterogeneity of announcements on behaviour ---------------------------------------------------------------

# Will want to control for own chocies in R1

models_announce <- list(
  "Sample:\nNo discussion\n(private)" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_self_selected_trans + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", discuss_type == "control"),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order"),
    cluster = "group_id",
    coef_omit = "stratum_id|Intercept|video"
  ),
  "Sample:\nNon-observers" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_self_selected_trans + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", discuss_type == "choosing_only", announce_before == 0),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order"),
    cluster = "group_id",
    coef_omit = "stratum_id|Intercept|video"
  ),
  "Sample:\nObservers" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_self_selected_trans + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", discuss_type == "choosing_only", announce_before == 1),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order"),
    cluster = "group_id",
    coef_omit = "stratum_id|Intercept|video"
  ),
  "Sample:\nNo discussion\n(private)\n+ Non-observers" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_self_selected_trans + p_other_selected_trans * public_non_observer + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "choosing_only" & announce_after == 1 | discuss_type == "control")),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order"),
    cluster = "group_id",
    coef_omit = "stratum_id|Intercept|video"
  ),
  "Sample:\nNo discussion\n(private)\n+ Observers" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_self_selected_trans + public_observer * p_other_selected_trans + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_with_announce %>%
      filter(pair_includes_trans == 1, phase == "phase_2",
             (discuss_type == "choosing_only" & announce_after == 0 | discuss_type == "control")),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order"),
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = "public_observer",
    control_group = list("control" = "public_observer"),
    same_group_spec = same_group_spec
  ),
  "Sample:\nNon-observers\n+Observers" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_self_selected_trans + public_observer * p_other_selected_trans + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_with_announce %>%
      filter(pair_includes_trans == 1, phase == "phase_2",
             (discuss_type == "choosing_only")),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order"),
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = "public_observer",
    control_group = list("public_non_observer" = "public_observer"),
    same_group_spec = same_group_spec
  )
)


tex_export(
  models_announce,
  file = "outputs/tables/effect_of_announce.tex",
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
  coef_omit = "stratum_id|Intercept|video_type|item_diff|r2_reliability",
  additional_header = vec_to_custom_header(
    c(" ", rep("Dep var: Chose transgender worker in private outcome round (=1) (Phase 2 only)", length(models_announce)))
  ),
  controls_row = TRUE
)


models_announce[[6]]$coefficients["p_other_selected_trans:public_observer"] %>%
  write_stat("outputs/stats/effect_of_announce.tex")

models_announce[[6]] %>%
  tidy() %>%
  filter(term == "p_other_selected_trans:public_observer") %>%
  pull(p.value) %>%
  write_stat("outputs/stats/effect_of_announce_p_val.tex")

r2_with_announce_for_plot <- r2_with_announce %>%
  filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "choosing_only" & announce_before == 1) | discuss_type == "control") %>%
  mutate(p_other_selected_trans = factor(p_other_selected_trans) %>% fct_relevel("0"),
         treat_type_r2_p_other_selected_trans = fct_cross(treat_type_r2, p_other_selected_trans) %>%
           fct_relevel("control:0")) %>%
  count_prop(treat_type_r2_p_other_selected_trans)


# Graph version
feols_custom(
  r2_choose_trans ~ p_self_selected_trans + treat_type_r2_p_other_selected_trans,
  data = r2_with_announce_for_plot,
  fixef = c("stratum_id", "video_type", "delivery_incentive_exp"),
  cluster = "group_id",
  coef_omit = "stratum_id|Intercept|video"
) %>%

  tidy_90 %>%
  filter(term != "p_self_selected_trans") %>%
  mutate(treatment = str_match_all(term, "treat_type_r2_p_other_selected_trans(choices_observer|control)\\:.*") %>% map_chr(2),
         p_other_selected = str_match_all(term, "treat_type_r2_p_other_selected_trans(choices_observer|control)\\:(.*)") %>% map_chr(3) %>% as.numeric()) %>%
  add_row(
    treatment = "control",
    p_other_selected = 0,
    estimate = 0,
    conf.low = 0,
    conf.high = 0
  ) %>%
  print_all %>%
  ggplot(aes(x = p_other_selected, y = estimate, colour = treatment)) +
  geom_point() +
  geom_line() +
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = 0, width = 0, linetype = "dashed")

# Figure doesn't look great - not enough power

# Try with assumption of linearity
announce_linear <- feols_custom(
  r2_choose_trans ~ p_self_selected_trans + p_other_selected_trans + p_other_selected_trans * treat_type_r2,
  data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "choosing_only" & announce_before == 1) | discuss_type == "control"),
  fixef = c("stratum_id", "video_type", "delivery_incentive_exp"),
  cluster = "group_id",
)

r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "choosing_only" & announce_before == 1) | discuss_type == "control") %>%
  select(r2_choose_trans, p_self_selected_trans, p_other_selected_trans, treat_type_r2) %>%
  count_nas()

announce_linear_no_extra <- feols_custom(
  r2_choose_trans ~ p_self_selected_trans,
  data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "choosing_only" & announce_before == 1) | discuss_type == "control"),
  fixef = c("stratum_id", "video_type", "delivery_incentive_exp"),
  cluster = "group_id",
)

r2_with_announce_predic <- r2_with_announce %>%
  filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "choosing_only" & announce_before == 1) | discuss_type == "control") %>%
  mutate(
    predict_with_announce = predict(announce_linear),
    predict_no_announce = predict(announce_linear_no_extra),
    resid = predict_with_announce - predict_no_announce
  )



ggplot(r2_with_announce_predic, aes(x = p_other_selected_trans, y = resid, colour = treat_type_r2_label)) +
  geom_jitter(height = 0, width = 0.02)

# Using RI for predicted value at 0.5 being sig different from control

# Get RI declaration for both discussion full and pair_includes_trans
ri_observer_vs_control <- get_ra_declaration(
  df = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "choosing_only")) %>% count_prop(observer),
  treat = "observer",
  within = "group_id"
)

# Get the stat for the difference between the two treatment groups
diff_observer_control <- function(df) {

  suppressWarnings(suppressMessages(
    observer_control_model <- feols_custom(
      r2_choose_trans ~ p_self_selected_trans + p_other_selected_trans + p_other_selected_trans * observer + item_diff + r2_reliability_diff * r2_reliability_shown ,
      data = df,
      fixef = c("stratum_id", "video_type", "delivery_incentive_exp"),
      cluster = "group_id"
    )
  ))

  coeffs <- observer_control_model$coefficients

  predic_control <- coeffs[["p_other_selected_trans"]] * 0.5

  predic_observer <- coeffs[["observer"]] +
    coeffs[["p_other_selected_trans"]] * 0.5 +
    coeffs[["p_other_selected_trans:observer"]] * 0.5

  diff <- predic_observer - predic_control
  return(diff)

}


diff_observer_control(
  r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "choosing_only"))
)


# ASYMMETRIC SOCIAL PRESSURE???
# When others are 50/50 on choosing a transgender, I face social pressure to choose trans more??
# This makes sense

set.seed(12345)
ri_p_05_diff <- conduct_ri_custom(
  test_function = diff_observer_control,
  declarations = list(ri_observer_vs_control),
  p = "two-tailed",
  data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "choosing_only")),
  sims = 1000,
  progress_bar = TRUE,
  assignments = list("observer")
)

ri2:::summary.ri(ri_p_05_diff)

ri2:::summary.ri(ri_p_05_diff)$two_tailed_p_value %>% write_stat("outputs/stats/effect_of_announce_p05.tex")
plot(ri_p_05_diff)


# Effect of announce on SOBs and discrimination ----------------------------------------------------------------------------------------------------------------

group_predic_w_announce <- group_predic %>%
  left_join(df_announcements_ind, by = c("ind_id", "discuss_type")) %>%
  tidylog::left_join(r1_choices_ind, by = c("group_id", "ind_id")) %>%
    left_join(r1_for_listeners, by = c("group_id", "ind_id"), suffix = c("", "listener")) %>%

  mutate(
    p_self_selected_trans = ifelse(is.na(p_self_selected_trans), median_na(p_self_selected_trans), p_self_selected_trans),
    p_other_selected_trans = coalesce(p_other_selected_trans, p_other_selected_trans_control, p_other_selected_trans_listener)
  )

# USE only control group as control
models_announce_sob_discrim <- list(
  "Predicted prob. of others\nselecting trans\n(within-group, outcome round)" = feols_custom(
    fml = group_predic_choose_trans ~ public_observer + item_diff + reliability_diff + reliability_shown + p_self_selected_trans,
    fixef = c("stratum_id", "video_type", "phase"),
    cluster = "group_id",
    lasso = FALSE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("discussion_full"),
      interact = NULL,
      group_control = FALSE
    ),
    data = group_predic_w_announce %>%
      filter(discuss_type == "choosing_only" & announce_after == 0 | discuss_type == "control") %>%
      filter(!is.na(group_predic_choose_trans))
  ),
"Predicted prob. of others\nselecting trans\n(within-group, outcome round)" = feols_custom(
    fml = group_predic_choose_trans ~ p_other_selected_trans + p_self_selected_trans + public_observer * p_other_selected_trans + item_diff + reliability_diff + reliability_shown + p_self_selected_trans,
    fixef = c("stratum_id", "video_type", "phase"),
    cluster = "group_id",
    lasso = FALSE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("discussion_full"),
      interact = NULL,
      group_control = FALSE
    ),
    data = group_predic_w_announce %>%
      filter(discuss_type == "choosing_only" & announce_after == 0 | discuss_type == "control") %>%
      filter(!is.na(group_predic_choose_trans))
  ),

  "Chose trans in private\noutcome round (=1)" = feols_custom(
    r2_choose_trans ~ public_observer + p_self_selected_trans,
    data = r2_with_announce %>%
      filter(pair_includes_trans == 1,
             (discuss_type == "choosing_only" & announce_after == 0 | discuss_type == "control")),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order"),
    cluster = "group_id",
    ri = FALSE
  ),
  "Chose trans in private\noutcome round (=1)" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_self_selected_trans + public_observer * p_other_selected_trans + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_with_announce %>%
      filter(pair_includes_trans == 1,
             (discuss_type == "choosing_only" & announce_after == 0 | discuss_type == "control")),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order", "phase"),
    cluster = "group_id",
    ri = FALSE
  )

)


models_announce_sob_discrim %>%
  tex_export(coef_omit = "stratum_id|Intercept|video_type|item_diff|reliability|p_self_selected_trans",
             file = "outputs/tables/effect_of_announce_sob_discrim.tex",
             coef_rename = coef_label,
             gof_map = fe_label_no_fe,
             controls_row = TRUE, stat_vec = "long",
             dep_means = list(
               "Mean: No discussion (private)" = "discuss_type == 'control'"
             )
  )

models_announce_sob_discrim[[1]] %>% get_coeff("public_observer") %>%
times_100 %>% 
write_stat("outputs/stats/effect_of_announce_sob_coeff.tex", digits = 1)

models_announce_sob_discrim[[1]] %>% get_p_val("public_observer") %>%
write_p_val("outputs/stats/effect_of_announce_sob_p.tex", digits = 2)

models_announce_sob_discrim[[3]] %>% get_coeff("public_observer") %>%
times_100 %>% 
write_stat("outputs/stats/effect_of_announce_discrim_private_coeff.tex", digits = 1)

models_announce_sob_discrim[[3]] %>% get_p_val("public_observer") %>%
write_p_val("outputs/stats/effect_of_announce_discrim_private_p.tex", digits = 2)

models_announce_sob_discrim[[4]] %>% get_coeff("p_other_selected_trans:public_observer") %>%
times_100 %>% 
write_stat("outputs/stats/effect_of_announce_discrim_het_coeff.tex", digits = 0)

models_announce_sob_discrim[[4]] %>% get_p_val("p_other_selected_trans:public_observer") %>%
write_p_val("outputs/stats/effect_of_announce_discrim_het_p.tex", digits = 2)



# Effect of announce by wedge (are you pushed up or down) ---------------------------------------------------------------

r2_with_announce %>%
  filter(choosing_only == 1) %>%
  hist_basic(p_self_selected_trans - p_other_selected_trans)

# Effect of listening ---------------------------------------------------------------

set.seed(12345)

models_listener <- list(
  "Sample:\nNo discussion (private)" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", discuss_type == "control"),
    fixef = c("stratum_id", "video_type"),
    cluster = "group_id",
    lasso = FALSE,
    ri = FALSE,
    coef_omit = "stratum_id|Intercept|video"
  ),
  "Sample:\nNo discussion (private)" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_self_selected_trans,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", discuss_type == "control"),
    fixef = c("stratum_id", "video_type"),
    cluster = "group_id",
    lasso = FALSE,
    ri = FALSE,
    coef_omit = "stratum_id|Intercept|video"
  ),
  "Sample:\nNo discussion (private)\n+ Listeners" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans * is_listener,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "control" | is_listener %in% 1)),
    fixef = NULL,
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = c("is_listener"),
    control_group = list("control" = c("is_listener")),
    same_group_spec = same_group_spec,
    quiet = TRUE
  ),
  "Sample:\nNo discussion (private)\n+ Listeners" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans * is_listener,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "control" | is_listener %in% 1)),
    fixef = c("stratum_id", "video_type"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(potential_controls = control_vars, t = c("is_listener"), interact = NULL, group_control = TRUE),
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = c("is_listener"),
    control_group = list("control" = c("is_listener")),
    same_group_spec = same_group_spec,
    quiet = TRUE
  ),
  "Sample:\nNon-observers\n+ Listeners" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_other_selected_trans * is_listener,
    data = r2_for_asymmetry %>% filter(public_non_observer == 1 | is_listener %in% 1, phase == "phase_2"),
    fixef = NULL,
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = c("is_listener"),
    control_group = list("public_non_observer" = c("is_listener")),
    same_group_spec = same_group_spec,
    quiet = TRUE
  ),
  "Sample:\nNon-observers\n+ Listeners" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_other_selected_trans * is_listener,
    data = r2_for_asymmetry %>% filter(public_non_observer == 1 | is_listener %in% 1, phase == "phase_2"),
    fixef = c("stratum_id", "video_type"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("is_listener", "control"),
      interact = NULL,
      group_control = TRUE
    ),
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = c("is_listener"),
    control_group = list("public_non_observer" = c("is_listener")),
    same_group_spec = same_group_spec,
    quiet = TRUE
  ),
  "Sample:\nNo discussion (private)\n+ Non-observers\n+ Listeners" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_other_selected_trans * public_non_observer + p_other_selected_trans * is_listener,
    data = r2_for_asymmetry %>% filter(public_non_observer == 1 | is_listener %in% 1 | discuss_type == "control", phase == "phase_2"),
    fixef = NULL,
    cluster = "group_id",
    lasso = FALSE,
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = c("is_listener|non_observer"),
    control_group = list("control" = c("is_listener", "public_non_observer")),
    same_group_spec = same_group_spec,
    quiet = TRUE
  ),
  "Sample:\nNo discussion (private)\n+ Non-observers\n+ Listeners" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_other_selected_trans * public_non_observer + p_other_selected_trans * is_listener,
    data = r2_for_asymmetry %>% filter(public_non_observer == 1 | is_listener %in% 1 | discuss_type == "control", phase == "phase_2"),
    fixef = c("stratum_id", "video_type"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("public_non_observer", "is_listener"),
      interact = NULL,
      group_control = TRUE
    ),
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = c("is_listener|public_non_observer"),
    control_group = list("control" = c("is_listener", "public_non_observer")),
    same_group_spec = same_group_spec,
    quiet = TRUE
  )

)

# GET P-VALUES FOR difference between observer and listener, for last 2 models
p_vals_listener_interaction <- list(
  "Sample:\nNo discussion (private)\n+ Non-observers\n+ Listeners" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_other_selected_trans * control + p_other_selected_trans * is_listener,
    data = r2_for_asymmetry %>% filter(public_non_observer == 1 | is_listener %in% 1 | discuss_type == "control", phase == "phase_2"),
    fixef = NULL,
    cluster = "group_id",
    lasso = FALSE,
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = c("is_listener"),
    control_group = list("public_non_observer" = c("is_listener", "control")),
    quiet = TRUE,
    same_group_spec = same_group_spec
  ),
  "Sample:\nNo discussion (private)\n+ Non-observers\n+ Listeners" = feols_custom(
    r2_choose_trans ~ p_other_selected_trans + p_other_selected_trans * control + p_other_selected_trans * is_listener,
    data = r2_for_asymmetry %>% filter(public_non_observer == 1 | is_listener %in% 1 | discuss_type == "control", phase == "phase_2"),
    fixef = c("stratum_id", "video_type"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("control", "is_listener"),
      interact = NULL,
      group_control = TRUE
    ),
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = c("is_listener"),
    control_group = list("public_non_observer" = c("is_listener", "control")),
    quiet = TRUE,
    same_group_spec = same_group_spec
  )
) %>%
  map_dbl(get_p_val, "p_other_selected_trans:is_listener")

tex_export(
  models_listener,
  file = "outputs/tables/effect_of_listening.tex",
  coef_rename = coef_label,
  column_widths = list(1, "4.5in"),
  gof_map = fe_label %>% filter(!str_detect(raw, "FE\\:")),
  coef_omit = vars_to_regex("stratum_id|Intercept|video_type", control_vars),
  add_rows = combine_add_rows(
    list_to_add_rows(
      list("Other controls" = c("X", "X", " ", "X", " ", "X", " ", "X"))
    ),
    list_to_add_rows(
      list("p-value: $\\pi_{-i}$ x Listener = $\\pi_{-i}$ x Non-observer" = c(rep(" ", 6), round(unname(p_vals_listener_interaction), 3)))
    )
  ),
  additional_header =c(" " = 1, vec_to_custom_header(
    rep(c("Dependent var: Chose trans in private outcome round (Phase 2 only)"), length(models_listener))
  ))
)

# TODO:  Asymmetric persuasion?

all_p_vals_listener_control <- c(
  models_listener[[3]] %>% get_p_val("p_other_selected_trans:is_listener"),
  models_listener[[4]] %>% get_p_val("p_other_selected_trans:is_listener"),
  models_listener[[7]] %>% get_p_val("p_other_selected_trans:is_listener"),
  models_listener[[8]] %>% get_p_val("p_other_selected_trans:is_listener")
) %>%
  mean_na() %>%
  write_stat("outputs/stats/p_val_listener_persuasion_control.tex", p_value = TRUE)

all_p_vals_listener <- c(
  models_listener[[5]] %>% get_p_val("p_other_selected_trans:is_listener"),
  models_listener[[6]] %>% get_p_val("p_other_selected_trans:is_listener"),
  p_vals_listener_interaction
)

all_p_vals_listener %>% max_na() %>% write_stat("outputs/stats/p_val_listener_persuasion_max.tex", digits = 3, p_value = TRUE)
all_p_vals_listener %>% min_na() %>% write_stat("outputs/stats/p_val_listener_persuasion_min.tex", digits = 3, p_value = TRUE)

models_listener[[5]] %>% get_coeff("p_other_selected_trans:is_listener") %>%
  times_100 %>%
{. / 2} %>%
  write_stat("outputs/stats/coeff_listener_persuasion.tex", digits = 0)


# Effect of listening - probit ---------------------------------------------------------------

models_listener_probit <- list(
  "Sample:\nNo discussion (private)" = glm(
    r2_choose_trans ~ p_other_selected_trans + stratum_id + video_type,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", discuss_type == "control"),
    family = quasibinomial(link = 'probit')
  ),
  "Sample:\nNo discussion (private)" = glm(
    r2_choose_trans ~ p_other_selected_trans + p_self_selected_trans + stratum_id + video_type,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", discuss_type == "control"),
    family = quasibinomial(link = 'probit')

  ),
  "Sample:\nNo discussion (private)\n+ Listeners" = glm(
    r2_choose_trans ~ p_other_selected_trans * is_listener,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "control" | is_listener %in% 1)),
    family = quasibinomial(link = 'probit')

  ),
  "Sample:\nNo discussion (private)\n+ Listeners" = glm(
    r2_choose_trans ~ p_other_selected_trans * is_listener + stratum_id + video_type,
    data = r2_with_announce %>% filter(pair_includes_trans == 1, phase == "phase_2", (discuss_type == "control" | is_listener %in% 1)),
    family = quasibinomial(link = 'probit')

  ),
  "Sample:\nNon-observers\n+ Listeners" = glm(
    r2_choose_trans ~ p_other_selected_trans + p_other_selected_trans * is_listener,
    data = r2_for_asymmetry %>% filter(public_non_observer == 1 | is_listener %in% 1, phase == "phase_2"),
    family = quasibinomial(link = 'probit')

  ),
  "Sample:\nNon-observers\n+ Listeners" = glm(
    r2_choose_trans ~ p_other_selected_trans + p_other_selected_trans * is_listener + stratum_id + video_type,
    data = r2_for_asymmetry %>% filter(public_non_observer == 1 | is_listener %in% 1, phase == "phase_2"),
    family = quasibinomial(link = 'probit')

  ),
  "Sample:\nNo discussion (private)\n+ Non-observers\n+ Listeners" = glm(
    r2_choose_trans ~ p_other_selected_trans + p_other_selected_trans * public_non_observer + p_other_selected_trans * is_listener,
    data = r2_for_asymmetry %>% filter(public_non_observer == 1 | is_listener %in% 1 | discuss_type == "control", phase == "phase_2"),
    family = quasibinomial(link = 'probit')
  ),
  "Sample:\nNo discussion (private)\n+ Non-observers\n+ Listeners" = glm(
    r2_choose_trans ~ p_other_selected_trans + p_other_selected_trans * public_non_observer + p_other_selected_trans * is_listener + stratum_id + video_type,
    data = r2_for_asymmetry %>% filter(public_non_observer == 1 | is_listener %in% 1 | discuss_type == "control", phase == "phase_2"),
    family = quasibinomial(link = 'probit')
  )
)


probit_n_participants <- list("Num. participants" = models_listener_probit %>%
  map("data") %>%
  map_int(~ n_distinct(.x$ind_id)) %>%
  as.character()
)

probit_n_groups <- list("Num. participants" = models_listener_probit %>%
  map("data") %>%
  map_int(~ n_distinct(.x$group_id)) %>%
  as.character()
)

tex_export(
  models_listener_probit,
  file = "outputs/tables/listener_probit.tex",
  coef_rename = coef_label,
  column_widths = list(4, "1.5in"),
  gof_map = fe_label %>% filter(!str_detect(raw, "FE\\:")),
  coef_omit = vars_to_regex("group_control|stratum_id|Intercept|video_type|delivery|pair_includes_trans_alt$", control_vars_except_items_reliability),
  additional_header =c(" " = 1, vec_to_custom_header(
    rep(c("Dependent var: Chose trans in private outcome round (Phase 2 only)"), length(models_listener))
  )),
  add_rows = combine_add_rows(
    list_to_add_rows(probit_n_participants),
    list_to_add_rows(probit_n_groups)
  )
)

# Reasons R2  ---------------------------------------------------------------
# reasons_for_choices$reason_text_collapse
reasons_for_choices_clean <- reasons_for_choices %>%
  group_by(across(c(group_id, round, category, reason_text_collapse, pair_includes_trans, all_of(id_vars)))) %>%
  summarise(
    reason_selected = any(reason_selected)
  ) %>%
  rename(reason_text = reason_text_collapse) %>%
  mutate(reason_text = reason_text %>%
    str_replace_all("I want to give opportunity / help the worker", "     I want to give opportunity / help the worker"))

reasons_for_choices_clean %>%
  filter(!(is_listener %in% 1)) %>%
  mutate(
    pair_includes_trans_label = ifelse(pair_includes_trans, "Choice includes trans", "Choice doesn't include trans"),
  ) %>%
  ungroup %>%
  mutate(
    category = fct_relevel(factor(category), c("Gender", "Pro-social", "Items", "Worker", "Worker details", "Negative", "Other reason"))
  ) %>%
  mutate(
    text_cat = interaction(reason_text, category) %>%
      fct_relevel(
        "Other reason.Other reason",
        "Negative about other worker.Negative",
        "Education.Worker details",
        "Experience.Worker details",
        "Speaks English.Worker details",
        "Reliability Score.Worker details",
        "Age.Worker",
        "Worker seems friendly.Worker",
        "Easy to relate to worker.Worker",
        "Worker seems reliable.Worker",
        "How the worker looks / photo.Worker",
        "Easy to talk with worker.Worker",
        "Better items.Items",
        "We shouldn't discriminate.Pro-social",
        "Worker seems poor.Pro-social",
        "They are also human.Pro-social",
        "     I want to give opportunity / help the worker.Pro-social",
        "Worker is female.Gender",
        "Worker is male.Gender",
        "Worker is transgender.Gender"
      )
  ) %>%
  bar_chart(
    x = text_cat,
    y = reason_selected,
    fill = discuss_type_label,
    flip = TRUE,
    perc = TRUE,
    facet = pair_includes_trans_label,
    return_data = TRUE
  ) %>%

  ggplot(aes(x = text_cat, y = y, fill = discuss_type_label)) +
  geom_col(
    width = 0.5, position = position_dodge(0.5),
    colour = "black", linewidth = 0.2
  ) +
  facet_wrap(~ pair_includes_trans_label) +
  scale_x_discrete(guide = ggh4x::guide_axis_nested(n.dodge = 1)) +
  labs(x = element_blank(), y = "% of choice-pairs with this reason cited", fill = element_blank()) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), expand = c(0, 0), limits = c(0, 0.65)) +
  coord_flip() +
  scale_fill_manual(values = c("#577590", "#6D6875", "#E5989B", "#F2A365")) +
  theme_bw() +
  theme(panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "top") +
  geom_errorbar(aes(ymin = ymin, ymax = ymax), width = 0, position = position_dodge(0.5))

ggsave("outputs/figs/reasons_for_choices_r2.pdf", width = 11, height = 12)

# Reasons R2 - summary ---------------------------------------------------------------

reasons_for_choices_summ <- reasons_for_choices %>%
  group_by(across(c(group_id, round, pair_includes_trans, discussion_full, video_type, stratum_id, category, matches("discuss_type")))) %>%
  summarise(reason_selected = any(reason_selected)) %>%
  ungroup %>%
  dups_report(group_id, round, pair_includes_trans, category) %>%

  mutate(
    discuss_type_pooled = if_else(discuss_type == "discussion_full" | discuss_type == "discussion_pair", "discussion", "no_discussion")
  ) %>%
  mutate(
    choice_type_label = ifelse(pair_includes_trans,
                               "Choice includes trans",
                               "Choice doesn't include trans")
  )

reasons_for_choices_summ %>% glimpse


reasons_for_choices %>%
  count_prop(pair_includes_trans, chose_trans)


reasons_for_choices_summ_chose_trans <- reasons_for_choices %>%
  filter(pair_includes_trans) %>%
  group_by(across(c(group_id, round, chose_trans, category, matches("discuss_type")))) %>%
  summarise(reason_selected = any(reason_selected)) %>%
  ungroup %>%
  dups_report(group_id, round, chose_trans, category) %>%

  mutate(
    discuss_type_pooled = if_else(discuss_type == "discussion_full" | discuss_type == "discussion_pair", "discussion", "no_discussion")
  ) %>%
  mutate(
    choice_type_label = ifelse(chose_trans, "Choice includes trans & chose transgender worker",
                               "Choice includes trans & didn't choose transgender worker")
  )


reasons_for_choices_summ_combine <- bind_rows(
  reasons_for_choices_summ,
  reasons_for_choices_summ_chose_trans
) %>%
  group_by(category, choice_type_label, discuss_type_pooled) %>%
  summarise(mean_cl_cluster(reason_selected, group_id)) %>%
  mutate(y = estimate, ymin = conf.low, ymax = conf.high)

reasons_for_choices_summ_combine %>%
  mutate(
    type = choice_type_label
  ) %>%
  ungroup %>%
  mutate(
    type = fct_relevel(factor(type), "Choice doesn't include trans", "Choice includes trans", "Choice includes trans & chose transgender worker", "Choice includes trans & didn't choose transgender worker")
  ) %>%
  mutate(discuss_type_pooled = ifelse(discuss_type_pooled == "discussion",
                                      "Discussion (pooled)", "No discussion (pooled)") %>%
    factor() %>% fct_relevel("Discussion (pooled)")) %>%
  mutate(category = fct_rev(category)) %>%
  ggplot(aes(x = category, y = y, fill = discuss_type_pooled)) +
  geom_col(width = 0.8, position = position_dodge(0.8)) +
  facet_wrap(~ type) +
  coord_flip() +
  scale_fill_brewer(palette = "Set2", direction = 1,
                    guide = guide_legend(reverse = TRUE)) +
  theme_bw() +
  geom_errorbar(aes(ymin = ymin, ymax = ymax), width = 0.2, position = position_dodge(0.8)) +
  theme(
    legend.position = "top",
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank()) +
  labs(y = "% of choices with this reason cited", x = "Reason category",
       fill = element_blank(),
       colour = element_blank(),
       pattern_colour = element_blank(),
       pattern_fill = element_blank(),
       pattern = element_blank()) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1))


ggsave("outputs/figs/reasons_for_choices_r2_summ.pdf", width = 8, height = 6)



reasons_for_choices_summ %>% bar_chart(x = pair_includes_trans, y = reason_selected,
                                       fill = discuss_type_pooled, facet = category, flip = FALSE, perc = TRUE)



reasons_for_choices_summ_wide <- reasons_for_choices_summ %>%
  pivot_wider(names_from = category, names_prefix = "reason_", values_from = reason_selected) %>%
  janitor::clean_names() %>%
  mutate(
    discussion_trans = as.numeric(pair_includes_trans * (discuss_type_pooled == "discussion")),
    discussion = discuss_type_pooled == "discussion"
  )

# P-value - are discussion participants more likely to cite a pro-social reason?
model_discussion_pro_social <- feols_custom(
  reason_selected ~ discussion_full + video_type,
  data = reasons_for_choices_summ %>% filter(category == "Pro-social") %>% filter(discuss_type %in% c("control", "discussion_full")),
  fixef = c("stratum_id")
)

p_val_discussion_pro_social <- model_discussion_pro_social %>%
  get_p_val("discussion_full") %>%
  write_stat("outputs/stats/p_val_discussion_pro_social.tex", p_value = TRUE)

coeff_discussion_pro_social <- model_discussion_pro_social %>%
  get_coeff("discussion_full")

coeff_discussion_pro_social %>%
  times_100 %>%
  write_stat("outputs/stats/coeff_discussion_pro_social.tex", digits = 0)

control_mean_discussion_pro_social <- reasons_for_choices_summ %>% filter(category == "Pro-social") %>% filter(discuss_type == "control") %>% get_mean(reason_selected)

(coeff_discussion_pro_social / control_mean_discussion_pro_social) %>%
  write_percentage("outputs/stats/perc_discussion_pro_social_ratio.tex", digits = 0)

reason_vars <- reasons_for_choices_summ_wide %>%
  select(matches("reason_")) %>%
  colnames()


# F-test style analysis
feols_custom(
  as.formula(paste0("discussion ~ ", fml_sum(reason_vars), " + pair_includes_trans * (", fml_sum(reason_vars), ")")),
  data = reasons_for_choices_summ_wide %>% filter(discuss_type %in% c("control", "discussion_full"))
) %>%
  tidy_90


# Become differentially less likely to talk about the worker characteristics, but no action on the prosocial stuff

reasons_for_choices_wide <- reasons_for_choices %>%
  group_by(across(c(group_id, round, pair_includes_trans, reason_text, matches("discuss_type")))) %>%
  summarise(reason_selected = any(reason_selected)) %>%
  pivot_wider(names_from = "reason_text",
              names_prefix = "reason_",
              values_from = "reason_selected") %>%
  janitor::clean_names() %>%
  mutate(
    discuss_type_pooled = if_else(discuss_type == "discussion_full" | discuss_type == "discussion_pair", "discussion", "no_discussion"),
    discussion_trans = as.numeric(pair_includes_trans * (discuss_type_pooled == "discussion")),
    discussion = discuss_type_pooled == "discussion"
  ) %>%
  ungroup

reason_vars_detail <- reasons_for_choices_wide %>%
  select(matches("reason_")) %>%
  colnames()


feols_custom(
  as.formula(paste0("discussion ~ ", fml_sum(reason_vars_detail), " + pair_includes_trans * (", fml_sum(reason_vars_detail), ")")),
  data = reasons_for_choices_wide %>% filter(discuss_type %in% c("control", "discussion_full"))
) %>%
  tidy_90 %>%
  print_all


# 1. Not enough power when each reason separately
# 2. No clear pattern when pooling the reasons into categories

# Correlation between R1 reasons and R2 reasons ---------------------------------------------------------------

combined_reasons %>% glimpse


combined_reasons_clean <- combined_reasons %>%
  filter(phase == "phase_2") %>%
  filter(discuss_type %in% c("discussion_full", "discussion_pair")) %>%
  count_prop(reason_heard_r1) %>%
  filter(reason_val > 0)


n_distinct_reasons <- combined_reasons$reason_val %>% n_distinct()

combined_reasons_clean$reason_heard_r1_trans


models_r1_r2_reasons <- list(
  "All choices" = feols_custom(
    reason_selected ~ reason_heard_r1,
    data = combined_reasons_clean,
    fixef = c("reason_val", "stratum_id", "video_type"),
    cluster = "group_id"
  ),
  "Trans choices only" = feols_custom(
    reason_selected ~ reason_heard_r1_trans,
    data = combined_reasons_clean %>% filter(pair_includes_trans),
    fixef = c("reason_val", "stratum_id", "video_type"),
    cluster = "group_id"
  )
)

tex_export(
  models_r1_r2_reasons,
  file = "outputs/tables/r1_r2_reasons_corr.tex",
  coef_rename = coef_label,
  gof_map = fe_label,
  additional_header = c(" " = 1, vec_to_custom_header(
    rep(c("Reason was cited about\noutcome round choice (=1)"), 2)
  )),
  add_rows = list_to_add_rows(list("Num. reasons" = rep(as.character(n_distinct_reasons), length(models_r1_r2_reasons))))
)

# # EXOGENOUS EFFECT OF PHOTOS ON DISCUSSION ---------------------------------------------------------------
#
# # some photos are more appealing, so if they are seen in R1 this exogenously makes the discussion more
# # positive, and I can see whether this has a differential effect on the treatment effect?

# 1. First, work out which trans photos are most appealing from control group
omitted_photo <- "T1.jpg"


r2_by_photo <- r2_choices %>%
  mutate(trans_photo_id = coalesce(
    ifelse(str_detect(r2_photo_1, "T"), r2_photo_1, NA_character_),
    ifelse(str_detect(r2_photo_2, "T"), r2_photo_2, NA_character_)
  )) %>%
  mutate(trans_photo_id = factor(trans_photo_id) %>% fct_relevel(omitted_photo))

# Get photo FEs for trans (only from control group)
photo_fes <- feols_custom(
  r2_choose_trans ~ video_type_placebo + video_type_treatment + factor(stratum_id) + delivery_incentive_exp + item_diff + r2_reliability_diff * r2_reliability_shown  + trans_photo_id,
  data = r2_by_photo %>% filter(discuss_type %in% c("control")) %>% filter(!is.na(r2_choose_trans)),
  fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair"),
  cluster = "group_id"
)

photo_fes_df <- photo_fes %>% tidy_90 %>%
  filter(str_detect(term, "trans_photo_id")) %>%
  mutate(term = term %>% str_replace("trans_photo_id", "")) %>%
  add_row(term = omitted_photo, estimate = 0, std.error = 0)

photo_fes_df %>%
  ggplot(aes(x = term, y = estimate, ymin = conf.low, ymax = conf.high)) +
  geom_errorbar() + geom_point()


# Add photo FEs to R1 choices
r1_w_photo_fes <- r1_choices %>%
  mutate(trans_photo_id = coalesce(
    ifelse(str_detect(r1_photo_1, "T"), r1_photo_1, NA_character_),
    ifelse(str_detect(r1_photo_2, "T"), r1_photo_2, NA_character_)
  )) %>%
  tidylog::left_join(photo_fes_df %>% select(trans_photo_id = term, trans_photo_fe = estimate),
                     by = c("trans_photo_id"))

# Validation check - do the photo FEs predict the R1 choices? (first stage)
feols_custom(
  r1_choose_comparator ~ (pair_includes_trans) * (video_type + discussion_full + factor(stratum_id)) + item_diff + quality_diff * quality_included + trans_photo_fe,
  data = r1_w_photo_fes %>% filter(discuss_type %in% c("control", "discussion_full"), !is.na(r1_choose_trans)),
  fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
  cluster = "group_id",
)


# Then, do they predict the R2 choices? (second stage)
# For this, need to amalgamate R1 FEs for each individual
r1_fes_ind <- r1_w_photo_fes %>%
  group_by(ind_id) %>%
  summarise(r1_trans_photo_fe = mean_na(trans_photo_fe))

# Then add to R2 choices, and also add R2 photo FEs
r2_with_r1_fes <- r2_by_photo %>%
  tidylog::left_join(r1_fes_ind, by = "ind_id") %>%
  tidylog::left_join(photo_fes_df %>% select(trans_photo_id = term, trans_photo_fe = estimate),
                     by = c("trans_photo_id"))


# Does the photo FE in R1 predict the R2 choices, and does it do so MORE STRONGLY in the treatment group?
# NEED TO control for the fact that it means that later FEs will be different - need to control for later FEs?
list(
  "No interaction" = feols_custom(
    r2_choose_trans ~ trans_photo_fe + r1_trans_photo_fe + video_type_placebo + video_type_treatment + discussion_full + factor(stratum_id) + delivery_incentive_exp + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_with_r1_fes %>% filter(discuss_type %in% c("control", "discussion_full")) %>% filter(!is.na(r2_choose_trans)),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id"
  ),
  "Interaction" = feols_custom(
    r2_choose_trans ~ trans_photo_fe + r1_trans_photo_fe * discussion_full + video_type_placebo + video_type_treatment + discussion_full + factor(stratum_id) + delivery_incentive_exp + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_with_r1_fes %>% filter(discuss_type %in% c("control", "discussion_full")) %>% filter(!is.na(r2_choose_trans)),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id"
  )
)

# DOESNT WORK - seems to be wrong way round

# Look at treatment effects for each photo ---------------------------------------------------------------

treat_by_photo <- tibble(
  photo_id = r2_by_photo %>% pull(trans_photo_id) %>% levels()
) %>%
  mutate(
    model = map(photo_id, ~ {
      feols_custom(
        r2_choose_trans ~ video_type_placebo + video_type_treatment + discussion_full + factor(stratum_id) + delivery_incentive_exp + item_diff + r2_reliability_diff * r2_reliability_shown ,
        data = r2_by_photo %>% filter(discuss_type %in% c("control", "discussion_full")) %>% filter(!is.na(r2_choose_trans), trans_photo_id == .x),
        fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
        cluster = "group_id",
        ri = FALSE
      )
    })
  )

treat_by_photo_clean <- treat_by_photo %>%
  mutate(
    coeffs = map(model, ~ tidy_90(.x) %>% filter(term == "discussion_full")),
  ) %>%
  unnest(coeffs) %>%
  mutate(
    photo_order = photo_id %>%
      str_replace("T", "") %>%
      str_replace("\\.jpg", "") %>%
      as.integer()
  ) %>%
  arrange(photo_order) %>%
  mutate(photo_order = row_number()) %>%
  mutate(
    photo_label = paste0("Worker ", photo_order, "\n(N=", n, ")"),
    photo_label = fct_reorder(factor(photo_label), photo_order) %>% fct_rev(),
  )


treat_by_photo_clean %>%
  ggplot(aes(x = photo_label, y = estimate, ymin = conf.low, ymax = conf.high)) +
  geom_pointrange(colour = "#1f78b4") +
  coord_flip() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  theme_bw() +
  theme(
    panel.grid = element_blank()
  ) +
  labs(x = "Transgender Worker Photo", y = "Effect of 3-person discussion")

ggsave("outputs/figs/treat_by_photo_clean.pdf", width = 6, height = 6)

treat_by_photo_clean

# Correlation between speakers' narratives and listener's choices  ---------------------------------------------------------------
# - evidence of narratives persuasion

# V2 - reasons categories
reasons_wide_for_listeners <- reasons_clean %>%
  filter(group_obs_trans_included) %>% # keep only trans reasons
  ungroup %>%
  select(-reason_text) %>%
  pivot_wider(names_from = category, values_from = reason_selected, names_prefix = "rsn_") %>%
  janitor::clean_names() %>%
  mutate(across(matches("^rsn_", ignore.case = FALSE), ~ map_lgl(.x, any))) %>%
  mutate(across(matches("^rsn_", ignore.case = FALSE), ~ as.numeric(.x))) %>%

  dups_report(group_id) %>%

  tidylog::left_join(
    r1_choices %>%
      filter(!is.na(r1_choose_trans)) %>%
      filter(is_speaker == 1) %>%
      select(group_id, r1_choose_trans, round) %>%
      mutate(round = as.character(round)) %>% dups_drop(),
    by = c("group_id", "round")
  ) %>%
  group_by(across(-matches("^rsn_"))) %>%
  summarise(across(matches("^rsn_"),
                   ~ sum_na(.x))) %>%

  # Merge with listeners
  tidylog::left_join(
    r2_choices_num %>% filter(is_listener == 1),
    by = "group_id",
    suffix = c("", "_extra")
  ) %>%

  tidylog::left_join(
    r1_n_choose_trans_group %>% select(-ind_id) %>% dups_drop(),
    by = "group_id",
    suffix = c("", "_extra")
  )

r1_n_choose_trans_group$n_r1_choose_trans_group


rsns_vec <- reasons_wide_for_listeners %>%
  ungroup %>%
  select(matches("^rsn_")) %>%
  select(-rsn_other_reason) %>%
  colnames()


listener_models <- rsns_vec %>%
  map(~ feols_custom(
    as.formula(paste0(
      "r2_choose_trans ~ item_diff + r2_reliability_diff * r2_reliability_shown + n_r1_choose_trans_group +",
      fml_sum(.x)
    )),
    data = reasons_wide_for_listeners %>% filter(pair_includes_trans==1),
    cluster = "ind_id",
    fixef = c("video_type", "stratum_id", "comparator_order_in_pair")
  )) %>%
  set_names(rsns_vec)

rsn_rename <- function(x) {
  x %>%
    str_remove("^rsn_") %>%
    str_replace_all("_", " ") %>%
    str_to_title()
}

listener_models_plot_data <- listener_models %>%
  modelplot(draw = FALSE)

listener_models_plot_data %>%
  filter(!str_detect(term, "item_diff|reliability|n_r1_choose_trans")) %>%
  mutate(term = rsn_rename(term)) %>%
  ggplot(aes(x = term, y = estimate, ymin = conf.low, ymax = conf.high, colour = term)) +
  geom_pointrange(show.legend = FALSE) +
  coord_flip() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  theme_bw() +
  theme(panel.grid.minor.y = element_blank(),
        panel.grid.major.y = element_blank()) +
  labs(y = "Coefficient estimate on [Listener chose trans in outcome round]\n(95% CI)", x = "Reason category")


ggsave("outputs/figs/reasons_to_listener.pdf", width = 6, height = 4)


listener_models_p <- c(
  listener_models$rsn_pro_social %>% get_p_val("rsn_pro_social"),
  listener_models$rsn_gender %>% get_p_val("rsn_gender")
)

listener_models$rsn_pro_social %>% get_coeff("rsn_pro_social") %>% times_100 %>% write_stat("outputs/stats/rsn_pro_social.tex", digits = 0)


# Do videos affect what reasons are cited in discussion? ---------------------------------------------------------------

reasons_clean_cat %>%
  group_by(category, group_obs_trans_included_label,
           video_type) %>%
  summarise(mean_cl_boot(reason_selected)) %>%

  mutate(video_type_label = fct_recode(video_type, "Legal Rights" = "treatment", "Rights Messaging" = "placebo", "Control" = "control")) %>%


  ungroup %>%
  mutate(
    type = group_obs_trans_included_label %>% factor() %>% fct_relevel("Choice doesn't include trans")
  ) %>%

  ggplot(aes(x = category, y = y, fill = video_type_label)) +
  geom_col(position = position_dodge(0.8), width = 0.8) +
  facet_wrap(~type) +
  geom_errorbar(aes(ymin = ymin, ymax = ymax), width = 0.2, position = position_dodge(0.8)) +
  coord_flip() +
  theme_bw() +
  scale_fill_discrete(guide = guide_legend(reverse = TRUE)) +
  theme(
    legend.position = "top",
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank()) +
  labs(y = "% of choices with this reason cited", x = "Reason category", fill = "Rights video:") +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), expand = c(0, 0), limits = c(0, 0.95))

ggsave("outputs/figs/reasons_r1_by_video.pdf", width = 7, height = 6, scale = 1)


# AMOUNT DISCUSSED vs DOMINATION CHOICES ---------------------------------------------------------------

amount_discussed_vs_domination <- tidylog::full_join(
  r1_dominates,
  discuss_obs %>% mutate(round = as.numeric(round)),
  by = c("group_id", "round"),
  suffix = c("", "_extra")
) %>%
  mutate(
    pair_type = case_when(
      pair_includes_trans == 1 ~ "Trans",
      pair_includes_female == 1 ~ "Female",
      TRUE ~ "Male"
    )
  )

amount_discussed_vs_domination %>%
  bar_chart(x = pair_type, y = amount_discussed, fill = dominate_status)


# Is amount discussed correlated with listeners choosing trans? ----------------------------------------------------------------------------------------------------------------


amount_discussed_listener <- feols_custom(
  r2_choose_trans ~ amount_discussed_z_trans + amount_discussed_z_non_trans + item_diff + r2_reliability_diff * r2_reliability_shown,
  data = r2_listener_w_amount_discussed,
  fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair"),
  cluster = "group_id"
)

amount_discussed_listener %>% list() %>% tex_export(coef_omit = vars_to_regex(c("stratum_id", "Intercept", "video", str_subset(control_vars, "r2_reliability", negate = TRUE), "delivery")))


# write_stat
amount_discussed_listener %>% get_coeff("amount_discussed_z_trans") %>% times_100 %>% write_stat("outputs/stats/coeff_amount_discussed_listener_trans.tex", digits = 0)
amount_discussed_listener %>% get_p_val("amount_discussed_z_trans") %>% write_stat("outputs/stats/amount_discussed_listener_trans.tex", p_value = TRUE, digits = 3)
amount_discussed_listener %>% get_p_val("amount_discussed_z_non_trans") %>% write_stat("outputs/stats/amount_discussed_listener_non_trans.tex", p_value = TRUE)


# COMPARE TO THE CONTROL GROUP when amount_discussed_trans is 0
amount_discussed_listener_control <- feols_custom(
  data = r2_choices_num %>% filter(discuss_type == "control" | is_listener %in% 1) %>%
    left_join(r1_amount_discussed, by = "group_id") %>%
    mutate(across(matches("amount_discussed"), ~ ifelse(is.na(.x) & discuss_type == "control", 0, .x))),
  fml = r2_choose_trans ~ discussion_pair + amount_discussed_trans + amount_discussed_non_trans + item_diff + r2_reliability_diff * r2_reliability_shown,
  fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair"),
  cluster = "group_id"
)

amount_discussed_listener_control %>% list() %>% tex_export(coef_omit = vars_to_regex(c("stratum_id", "Intercept", "video", str_subset(control_vars, "r2_reliability", negate = TRUE), "delivery")))


r2_listener_w_amount_discussed %>%
  tidylog::left_join(r1_n_choose_trans, by = "group_id") %>%
  mutate(n_r1_choose_trans_group = ifelse(n_r1_choose_trans_group >= 1, 1, 0)) %>%

  mutate(n_r1_choose_trans_group = factor(n_r1_choose_trans_group)) %>%
  bar_chart(
    x = amount_discussed_trans,
    y = r2_choose_trans,
    fill = n_r1_choose_trans_group,
    width = 0.2
  )

r2_listener_w_amount_discussed %>%
  tidylog::left_join(r1_n_choose_trans, by = "group_id") %>%
  mutate(n_r1_choose_trans_group = ifelse(n_r1_choose_trans_group >= 1, 1, 0)) %>%
  mutate(n_r1_choose_trans_group = factor(n_r1_choose_trans_group)) %>%
  ggplot(aes(x = amount_discussed_trans, y = r2_choose_trans, colour = n_r1_choose_trans_group, fill = n_r1_choose_trans_group)) +
  geom_smooth(method = "lm")

# r1_n_choose_trans %>% filter(discuss_type == "discussion_pair") %>%
#   view()


# Even when amount discussed is minimal, does it still affect listener's behavior? ----------------------------------------------------------------------------------------------------------------

# Amount discussed
# gobs6 (select_one) How much discussion occurred?
# Options: 0 = No discussion (e.g., only choice is made) 1 = Very little discussion (e.g., one person mentions one reason) 2 = Some discussion (e.g., multiple persons mention one reason for choice) 3 = Lots of discussion (e.g., multiple persons mention multiple reasons for choice)

discuss_obs %>% filter(discuss_type == "discussion_pair") %>%
  select(matches("amount_discussed")) %>%
  count_prop(amount_discussed) # only 3% have "No discussion"

# Do it in "absolute terms" - i.e., not z-scored
amount_discussed_listener <- feols_custom(
  r2_choose_trans ~ amount_discussed_trans + amount_discussed_non_trans + item_diff + r2_reliability_diff * r2_reliability_shown,
  data = r2_listener_w_amount_discussed,
  fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair"),
  cluster = "group_id"
)